package com.meihua.baoleyi.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.CircularImage;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.baoleyi.utils.UrlContants;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liming on 2017/10/17.
 * 我的钱包
 */

public class MyWalletActivity extends BaseActivity {

    private RelativeLayout mRl_balance;//余额
    private RelativeLayout mRl_bill;//账单
    private RelativeLayout mRl_bankcard;//银行卡
    private RelativeLayout mRl_consumer_rebates;//消费返利
    private CircularImage mAvatar;
    private TextView mName;
    private ImageView ivVip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        ButterKnife.bind(this);
        init();
    }

    public static void startAction(Context context, int level) {
        Intent intent = new Intent(context, MyWalletActivity.class);
        intent.putExtra("level", level);
        context.startActivity(intent);
    }

    /**
     * 初始化数据
     */
    private void init() {
        int level = getIntent().getIntExtra("level", 0);

        mRl_balance = (RelativeLayout) findViewById(R.id.rl_yue);//余额
        mRl_bill = (RelativeLayout) findViewById(R.id.rl_zhangdan);//账单
        mRl_bankcard = (RelativeLayout) findViewById(R.id.rl_bankcard);//银行卡
        mRl_consumer_rebates = (RelativeLayout) findViewById(R.id.rl_fanli);//消费返利
        ivVip = findViewById(R.id.ivVip);
        //消费返利
        mAvatar = (CircularImage) findViewById(R.id.iv_round);
        mName = (TextView) findViewById(R.id.textView);
        mRl_balance.setOnClickListener(this);
        mRl_bill.setOnClickListener(this);
        mRl_bankcard.setOnClickListener(this);
        mRl_consumer_rebates.setOnClickListener(this);
        mName.setText(MyApp.getInstance().getUname());
        Glide.with(this).load(MyApp.getInstance().getAvatar()).placeholder(R.mipmap.chanpin).into(mAvatar);
        if (level == 0) {
            ivVip.setVisibility(View.GONE);
        } else if (level == 1) {
            ivVip.setVisibility(View.VISIBLE);
            ivVip.setImageResource(R.mipmap.zhongjidaogou);
        } else if (level == 2) {
            ivVip.setVisibility(View.VISIBLE);
            ivVip.setImageResource(R.mipmap.gaojidaogou);
        } else {
            ivVip.setVisibility(View.VISIBLE);
            ivVip.setImageResource(R.mipmap.hehuoren);
        }
    }

    /**
     * 点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish(); //返回
                break;
            case R.id.rl_yue://余额
                startActivity(new Intent(this, MyMoneyBagActivity.class));
                break;
            case R.id.rl_zhangdan://账单
                Intent in = new Intent(MyWalletActivity.this, MyWalletBillActivity.class);
                in.putExtra("url", UrlContants.BILL_MYMONEY);
                startActivity(in);
                break;
            case R.id.rl_bankcard://银行卡
                bankcardApprove();
                break;
            case R.id.rl_fanli://消费返利
                startActivity(new Intent(this, ConsumerRebateActivity.class));
                break;
        }
    }

    /**
     *  验证用户身份是否认证
     */
    private void bankcardApprove() {
        showDialog();

        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        treeMap.put("user_id", MyApp.getInstance().getUser_id());

        NetHelper.post(APIServer.BANKCARD_APPROVE, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                JSONObject object;
                try {
                    object = new JSONObject(s);
                    if (object.optString("code").equals("200")) {
                        //认证成功 跳转到银行卡界面
                        startActivity(new Intent(MyWalletActivity.this, BankActivity.class));
                    } else {
                        ToastUtil.show(MyWalletActivity.this, object.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dismissDialog();
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}




